package com.qf.service.impl;

import com.qf.Dao.GameRepository;
import com.qf.pojo.BaseResp;
import com.qf.pojo.Game;
import com.qf.service.GameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GameServiceImpl implements GameService {

    @Autowired
    private GameRepository gameRepository;

    @Override
    public BaseResp add(Game game) {
        BaseResp baseResp = new BaseResp();
        if(game.getId() == null){
            baseResp.setMgs("新增成功");
        }else {
            baseResp.setMgs("修改成功");
        }
        baseResp.setCode(200);
        gameRepository.saveAndFlush(game);
        return baseResp;
    }

    @Override
    public BaseResp del(Integer id) {
        BaseResp baseResp = new BaseResp();
        gameRepository.deleteById(id);
        baseResp.setCode(200);
        baseResp.setMgs("删除成功");
        return baseResp;
    }

    @Override
    public BaseResp selectAll(Integer page,Integer size) {
        BaseResp baseResp = new BaseResp();
        Pageable pageRequest = PageRequest.of(page - 1, size);
        Page<Game> all = gameRepository.findAll(pageRequest);
        baseResp.setCode(200);
        baseResp.setTotal(all.getTotalElements());
        baseResp.setData(all.getContent());
        return baseResp;
    }
}
